package com.rongshu.view.login.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.rongshu.api.RetorfitService;
import com.rongshu.utils.Common;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.login.contract.ResetPasswordView;
import com.rongshu.view.login.entity.PasswordLogin;
import com.rongshu.view.login.entity.ResetPassword;
import com.rongshu.view.login.entity.UpdateUserClass;

import java.util.HashMap;
import java.util.Map;

import rx.Subscriber;
import rx.functions.Action0;

public class ResetPasswordPresenter extends BasePresenter<ResetPasswordView> {

    public ResetPasswordPresenter(Context context) {
        super(context);
    }

    /**
     * 忘记密码
     */
    public void postResetPassword(String mobile, String sms_code, String sign, UpdateUserClass updateUserClass) {
        RetorfitService.postResetPassword(mobile, sms_code, sign, updateUserClass)
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {

                    }
                })
                .subscribe(new Subscriber<ResetPassword>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ResetPassword resetPassword) {
                        getView().onResetPassword(resetPassword);
                    }
                });
    }

    /**
     * 通过手机号码，密码登陆
     */
    public void getUserRegisterforMobilePwd(String phone, String pwd) {
        //极光注册id
        String registerId = UserInfoUtils.getRegistrationId();
        Map<String, Object> map = new HashMap<>();
        map.put("mobile", phone);
        map.put("pwd", pwd);
        map.put("registrationid", TextUtils.isEmpty(registerId) ? "" : registerId);
        RetorfitService.getUserRegisterforMobilePwd(phone, pwd, registerId, Common.encryptionSign(map))
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {

                    }
                })
                .subscribe(new Subscriber<PasswordLogin>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(PasswordLogin passwordLogin) {
                        getView().getUserRegisterforMobilePwd(passwordLogin);
                    }
                });
    }
}
